# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=crypto++
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.4.0
pkgrel=2
pkgdesc="Crypto++ Library is a free C++ class library of cryptographic schemes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.cryptopp.com/"
license=('Boost Software License 1.0')
options=('staticlibs' 'strip' 'emptydirs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "unzip")
source=("https://www.cryptopp.com/cryptopp${pkgver//./}.zip"
        #https://github.com/weidai11/cryptopp/archive/CRYPTOPP_${pkgver//./_}.tar.gz
        libcrypto++.pc
        cryptopp-5.6.5-cmake.patch)
sha256sums=('c0f5e5cd2c693c8160e9c51666e95949a1c19fd4fe4fef874af2ec1e42757b9a'
            '60df56dcf354e8e1b24b90d460dc299c6991eb558a7068d154cb4f31be25edbd'
            '2c588cb085749b76adc28be5075b4978c8822f1e0327ccc06d823a1d436f56e9')
noextract=(cryptopp${pkgver//./}.zip)

prepare() {
  [[ -d ${srcdir}/${_realname} ]] && rm -rf ${srcdir}/${_realname}
  unzip ${srcdir}/cryptopp${pkgver//./}.zip -d ${srcdir}/${_realname}

  cd ${srcdir}/${_realname}
  sed -i -e 's/^CXXFLAGS/#CXXFLAGS/' GNUmakefile
  patch -p1 -i ${srcdir}/cryptopp-5.6.5-cmake.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DDISABLE_SSSE3=ON \
    -DDISABLE_ASM=ON \
    ../${_realname}

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make test || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  # Install pkgconfig file
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  install -Dm644 "${srcdir}/libcrypto++.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libcrypto++.pc"
  sed -e "s|@PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@VERSION@|${pkgver}|g" \
      -i "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libcrypto++.pc"

  # Remove cryptest.exe and test files, only needed for check() and bloats the package
  # because cryptest.exe is linked statically.
  rm "${pkgdir}/${MINGW_PREFIX}/bin/cryptest.exe"
  rm -r "${pkgdir}/${MINGW_PREFIX}/share/cryptopp/"

  install -Dm644 ${srcdir}/${_realname}/License.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
